import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

# 1. 读取并准备数据
# 定义特征名称
feature_names = [
    "Alcohol", "Malic_acid", "Ash", "Alcalinity_of_ash", "Magnesium",
    "Total_phenols", "Flavanoids", "Nonflavanoid_phenols", "Proanthocyanins",
    "Color_intensity", "Hue", "OD280/OD315_of_diluted_wines", "Proline"
]

# 1.读取数据（假设已下载到本地）
# 如果未下载，可以使用urllib库从URL直接读取
try:
    # 尝试从本地文件读取
    data = pd.read_csv("wine.data", header=None, names=["class"] + feature_names)
except FileNotFoundError:
    # 从UCI网站直接读取
    import urllib.request
    url = "https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data"
    response = urllib.request.urlopen(url)
    data = pd.read_csv(response, header=None, names=["class"] + feature_names)
print(data)